package com.tenbit.tbw.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.tenbit.tbw.vo.EMP1030VO;
import com.tenbit.tbw.vo.EMP1010VO;
import com.tenbit.tbw.vo.EMP1020VO;
import com.tenbit.tbw.vo.EMP1040VO;

@Repository
public class EmployeeCodebookEDAO
{
	@Autowired
	private SqlSession sqlSession;

	public List<EMP1010VO> getEmployeeCodebookList()
	{
		List<EMP1010VO> mv= sqlSession.selectList("getEmployeeCodebook");
		
		return mv;
	}
	
	public List<EMP1020VO> getEmployeeList(int page, int limit)
	{
		List<EMP1020VO> mv; 
		int startrow = (page-1)*10+1;
		int endrow=startrow+limit-1;
		
		Map<String, Integer> map = new HashMap<String, Integer>();
		
		map.put("startrow",startrow);
		map.put("endrow", endrow);
		
		mv = sqlSession.selectList("getEmployeeList", map);
		return mv;
	}

	public int getTotal() {
		int totalNum=0;
		totalNum = (Integer) sqlSession.selectOne("getTotalNum");
		return totalNum;
	}

	public int getSearchTotal(Map<String, Object> map) {
		int totalNum=0;
		totalNum = (Integer) sqlSession.selectOne("getSearchTotalNum",map);
		return totalNum;
	}

	public List<EMP1020VO> getSearch(int page, int limit,
			Map<String, Object> map) {
		
		List<EMP1020VO> mv; 
		
		int startrow = (page-1)*10+1;
		int endrow=startrow+limit-1;
		
		map.put("startrow",startrow);
		map.put("endrow", endrow);
		
		mv = sqlSession.selectList("getSearchList",map);
		return mv;
		
	}

	public List<EMP1030VO> getInfo(int empCode) {
		
		List<EMP1030VO> empInfo=sqlSession.selectList("getEmployeeInfo",empCode);
		
		return empInfo;
	}

	public List<EMP1040VO> getSkillList(int empCode) {
		List<EMP1040VO> skills= sqlSession.selectList("getSkills",empCode);
		return skills;
	}

	public boolean setEmpInfo(Map<String, Object> map) {
		boolean result=false;
		int a=0;
		int b=0;
		int c=0;
		String[] skills = (String[])map.get("skillCode");
		
		a=sqlSession.delete("skillCodeDelete", map);
		
		for(int i=0;i<skills.length;i++){
			String skill = skills[i];
			map.put("skill", skill);
			c=sqlSession.insert("skillCodeInsert",map);
		}
		
		b=sqlSession.update("setEmployeeInfo", map);

		if(a>0 && b>0 && c>0){
			result=true;
		}
		return result;
	}

	public boolean deleteEmpInfo(int empCode) {
		boolean result=false;
		int a=0;
		int b=0;
		
		a=sqlSession.delete("skillDelete", empCode);
		b=sqlSession.delete("empInfoDelete", empCode);
				
		if(a>0 && b>0){
			result=true;
		}
		return result;
	}

	public boolean insertEmpInfo(Map<String, Object> map) {
		boolean result=false;
		int a=0;
		int b=0;
		
		System.out.println("dao");
		System.out.println(map);
		
		String[] skills = (String[])map.get("skillCode");
		
		a=sqlSession.insert("insertEmployeeInfo",map);
		
		for(int i=0;i<skills.length;i++){
			String skill = skills[i];
			map.put("skill", skill);
			b=sqlSession.insert("insertSkill",map);
		}

		if(a>0 && b>0){
			result=true;
		}
		return result;
	}

	public Integer getEmpNum(String name) {
		// TODO Auto-generated method stub
		return (Integer) sqlSession.selectOne("getEmpNum", name);
	}

	public List<EMP1020VO> getEmpList(int page, int limit, String name) {
		Map map= new HashMap();
		int startrow = (page-1)*10+1;
		int endrow=startrow+limit-1;
		
		map.put("name", name);
		map.put("startrow",startrow);
		map.put("endrow", endrow);
		
		List<EMP1020VO> list = sqlSession.selectList("getEmpList",map);
		
		return list;
	}


}
